package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.main.AloneThingBean;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3BottomAloneThingAdapter extends RecyclerView.Adapter<X3BottomAloneThingAdapter.BottomAloneThingHolder> {

    private List<AloneThingBean> mList;

    private List<Boolean> sList;

    private Context mContext;

    public X3BottomAloneThingAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
        this.sList = new ArrayList<>();
    }

    public void changeAloneThing(List<AloneThingBean> mList,String answer) {
        this.mList = mList;
        this.sList.clear();

        String[] split = null;
        if (!X3StringUtils.isEmpty(answer)) {
            split = answer.split(",");
        }

        for (int i = 0; i < mList.size(); i++){
            if (split == null || split.length == 0) {
                sList.add(false);
            }else {
                boolean isHave = false;
                for (int j = 0; j < split.length; j++) {
                    if (mList.get(i).thing.trim().equals(split[j].trim())) {
                        isHave = true;
                        break;
                    }
                }
                sList.add(i,isHave);
            }
        }

        List<String> nameList = new ArrayList<>();
        for (int i = 0; i < sList.size(); i++) {
            if (!sList.get(i)) {
                continue;
            }
            nameList.add(mList.get(i).thing);
        }
        if (iOnSelectThing != null) {
            iOnSelectThing.iOnSelectThing(nameList);
        }
        notifyDataSetChanged();
    }

    public void addAloneThing(String thing) {
        this.mList.add(new AloneThingBean(0,thing));
        this.sList.add(true);
        notifyItemChanged(mList.size() - 1);

        List<String> nameList = new ArrayList<>();
        for (int i = 0; i < sList.size(); i++) {
            if (!sList.get(i)) {
                continue;
            }
            nameList.add(mList.get(i).thing);
        }
        if (iOnSelectThing != null) {
            iOnSelectThing.iOnSelectThing(nameList);
        }
    }

    @NonNull
    @Override
    public BottomAloneThingHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new BottomAloneThingHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_alone_thing_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull BottomAloneThingHolder holder, int position) {
        holder.tv_name.setText(mList.get(position).thing + "");
        holder.iv_select.setVisibility(sList.get(position) ? View.VISIBLE : View.INVISIBLE);

        holder.itemView.setOnClickListener(v -> {
            Boolean aBoolean = sList.get(position);
            sList.remove(position);
            sList.add(position,!aBoolean);

            notifyDataSetChanged();

            List<String> nameList = new ArrayList<>();
            for (int i = 0; i < sList.size(); i++) {
                if (!sList.get(i)) {
                    continue;
                }
                nameList.add(mList.get(i).thing);
            }
            if (iOnSelectThing != null) {
                iOnSelectThing.iOnSelectThing(nameList);
            }
        });

    }

    IOnSelectThing iOnSelectThing;

    public void setiOnSelectThing(IOnSelectThing iOnSelectThing) {
        this.iOnSelectThing = iOnSelectThing;
    }

    public interface IOnSelectThing{
        void iOnSelectThing(List<String> nameList);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class BottomAloneThingHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        ImageView iv_select;
        public BottomAloneThingHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            iv_select = itemView.findViewById(R.id.iv_select);
        }
    }
}
